//
//  JSXFMDB.m
//  SmartPhoto
//
//  Created by 蒋尚秀 on 16/4/1.
//  Copyright © 2016年 蒋尚秀. All rights reserved.
//

#import "JSXFMDB.h"

@interface JSXFMDB ()

@property (nonatomic,strong) FMDatabase * database;

@end

@implementation JSXFMDB

+(instancetype)sharedJSXFMDB
{
    static dispatch_once_t onceToken;
    static JSXFMDB *jsxFMDB = nil;
    dispatch_once(&onceToken, ^{
       
        jsxFMDB = [[JSXFMDB alloc] init];
    });
    
    return jsxFMDB;
}

-(instancetype)init
{
    if (self = [super init]) {
        
        NSString * path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"CheckDB.db"];
        
        NSLog(@"%@",path);
        
        _database = [FMDatabase databaseWithPath:path];
        //打开数据库
        if ([_database open]) {
            NSLog(@"数据库打开成功");
            //创建表格
            NSString * create = @"create table if not exists CheckTable(date varchar primary key,week varchar)";
            
            if ([_database executeUpdate:create]) {
                NSLog(@"表格创建成功");
                //操作完成后将数据库关闭，再次使用时再打开
                [_database close];
                NSLog(@"数据库关闭");
            }
        }
    }
    return self;
}

-(BOOL)insertCheck:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    NSString *week = [self week:date];
    //先打开数据库
    if ([_database open]) {
        NSString * insert = @"insert into CheckTable(date,week) values(?,?)";
        if ([_database executeUpdate:insert,dateStr,week]) {
            NSLog(@"数据插入成功");
            [_database close];
            NSLog(@"数据库关闭");
            return true;
        }
    }
    return false;
}

-(NSArray *)selectAll
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    if ([_database open]) {
        NSString *select = @"select * from CheckTable order by date desc";
        FMResultSet * set = [_database executeQuery:select];
        while ([set next]) {
            NSString *date = [set stringForColumn:@"date"];
            NSString *week = [set stringForColumn:@"week"];
            [array addObject:@{@"date":date,@"week":week}];
        }
        [_database close];
    }
    
    return array;
}

-(int)selectTodayCheck
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
    if ([_database open]) {
        NSString * select = [NSString stringWithFormat:@"select * from CheckTable where date='%@'",dateStr];
        FMResultSet * set = [_database executeQuery:select];
        if ([set next]) {

            [_database close];
            NSLog(@"数据库关闭");
            return 1;
        }
        
        [_database close];
        NSLog(@"数据库关闭");
        return 0;
    }
    NSLog(@"数据库打开失败");
    return -1;
}

//删
-(BOOL)deleteCheck:(NSDate *)date
{
    if ([self selectTodayCheck]) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
        NSString *dateStr = [dateFormatter stringFromDate:date];
        if ([_database open]) {
            NSString *delete = [NSString stringWithFormat:@"delete from CheckTable where date='%@'",dateStr];
            if([_database executeUpdate:delete])
            {
                NSLog(@"删除成功");
                [_database close];
                return YES;
            }
            
        }
    }
    return NO;
}

-(BOOL)deleteCheckWith:(NSString *)dateStr
{

    if ([_database open]) {
        NSString *delete = [NSString stringWithFormat:@"delete from CheckTable where date='%@'",dateStr];
        if([_database executeUpdate:delete])
        {
            NSLog(@"删除成功");
            [_database close];
            return YES;
        }
        
    }
    return NO;
}

-(NSString *)week:(NSDate *)date
{
    NSArray * arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    comps = [calendar components:unitFlags fromDate:date];
    return arrWeek[[comps weekday]-1];
}


@end
